package com.linxianqin.resilience4j.controller.bulkhead;

import com.linxianqin.resilience4j.domain.DemoBDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @version 1.0
 * @className: BackendBController
 * @description: TODO
 * @author: lxq
 * @date: 2021/8/16
 **/
@Slf4j
@RestController
@RequestMapping(value = "/bulkhead/backendC")
public class BulkheadBackendCController {

    private final Map<String, DemoBDto> map = new HashMap<>();

    private AtomicInteger count = new AtomicInteger(0);

    @PostConstruct
    private void init() {
        map.put("1", DemoBDto.of("1", "demoB id=1,测试舱壁-backendC"));
        map.put("2", DemoBDto.of("2", "demoB id=2,测试舱壁-backendC"));
        map.put("3", DemoBDto.of("3", "demoB id=3,测试舱壁-backendC"));
    }

    @GetMapping("{aId}")
    public ResponseEntity<DemoBDto> getDemoBDto(@PathVariable String aId) throws InterruptedException {
        DemoBDto orDefault = this.map.getOrDefault(aId, new DemoBDto());
        try{
            Thread.sleep(3000);
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("服务B接口|舱壁模式|请求成功.第[{}]",count.incrementAndGet());
        return ResponseEntity.ok(orDefault);
    }
}
